package com.heima.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

//forward 转发
@Controller
@RequestMapping("/hello")
public class UserController {

    //无转发 但是效果跟转发一样
    @RequestMapping("textModelAndView")
    public ModelAndView textModelAndView(ModelAndView modelAndView){
        modelAndView.addObject("nowDate",new Date());
        modelAndView.addObject("msg","hello");
        modelAndView.setViewName("message");

        return modelAndView;
    }

    //返回页面jsp 转发到message.jsp
    @RequestMapping("textModelAndView1")
    public ModelAndView textModeAndView1(ModelAndView modelAndView){
        //设置模型数据
        modelAndView.addObject("nowDate",new Date());
        modelAndView.addObject("msg","hello");
        //设置视图
        modelAndView.setViewName("forward:/pages/message.jsp");
        return modelAndView;
    }

    //返回值为视图 转发到message.jsp
    @RequestMapping("testModel")
    public String testModel(Model model){
        model.addAttribute("nowDate",new Date());
        model.addAttribute("msg","hello Model");
        return "forward:/pages/message.jsp";
    }

    //返回值为视图 转发到message.jsp
    @RequestMapping("testModelMap")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("nowDate",new Date());
        modelMap.addAttribute("msg","hello ModelMap");
        return "forward:/pages/message.jsp";
    }

    //重定向 redirect 可以重定向到新的资源，站内站外均可
    @RequestMapping("testRedirect")
    public String testRedirect(){
        //重定向站外
        return "redirect:http://www.baidu.com";
    }

    @RequestMapping("testRedirect1")
    public String textRedirect1(){
        //重定向站内
        return "redirect:/pages/message.jsp";
    }
}
